<?php

namespace app\qsapi\controller;

use app\qsapi\service\BoxAdService;
use app\qsapi\service\GameService;
use app\qsapi\service\HomeService;
use app\qsapi\service\IosReviewService;
use think\cache\driver\Redis;
use think\Env;
use think\response\Json;

class Index extends BaseController
{

    protected $noNeedLogin=['index','getiosstartoff','hotKeys','opengame','openGame','resource','opentime','hotkeys'
        ,'getopenstatus','getopentime','tabresource','getenv','gettime','getwechatanddownloadqrcode','download','demo'];
    /**
     * 首页
     * @return Json
     */
    public function index(): Json
    {
        $param = $this->request->header('Version');
        return json((new HomeService())->home($param));
    }

    /**
     * 资源位接口
     * @return Json
     */
    public  function resource(): Json
    {
        $gameService = new BoxAdService();
        return json($gameService->getNewResource());
    }

    /**
     * Desc : 获取tab栏
     * User : you name
     * Date : 2024-01-15 14:05
     * @return Json
     */
    public function tabResource(): Json
    {
        $param = $this->request->header('Version');
        return json((new BoxAdService())->getTab($param));
    }

    /**
     * 开服时间
     * @return void
     */

    public  function openTime(){
        return json((new HomeService())->openTime());
    }

    /**
     * 开服时间(千年特殊接口)
     * @return void
     */
    public function getOpenTime(){
        return json((new HomeService())->getOpenTime($this->request->get()));
    }

    /**
     * 热搜词
     * @return Json
     */
    public function hotKeys(): Json
    {
        return json((new HomeService())->getHostKeys());
    }

    /**
     * 开服游戏
     * @return Json
     */
    public function openGame(): Json
    {
        $gameService = new GameService();
        return json($gameService->openGame($this->request->get()));
    }

    /**
     * 获取环境模式
     * @return Json
     */
    public function getEnv(){
        $info = (new IosReviewService())->getEnv();
        if (empty($info)){
            return json(api_message(["env"=>1], "操作成功"));
        }
        return json(api_message(["env"=>$info->env], "操作成功"));
    }

    /**
     * 获取游戏启动按钮状态
     * @return array|void
     */
    public function getOpenStatus(){
        write_log('INFO', 'Base.php', '请求参数:'.json_encode($this->request->post()), 'get_open_status', true);
        $iosReviewService = new IosReviewService();
        $bundle_id = $this->request->post("bundle_id") ?? "";
        $version = $this->request->post("version") ?? "";
        if (empty($bundle_id) || empty($version)){
            return json(api_message([], "参数不能为空", 0));
        }
        $result = $iosReviewService->getOpenStatus($bundle_id, $version);
        if (empty($result)){
            return json(api_message(["flag" => false], "操作成功", 200));
        }
        return json(api_message(["flag" => true], "操作成功", 200));
    }

    /**
     * @Notes:获取当前时间
     * @Interface getTime
     * @return Json
     */
    public function getTime(): Json
    {
        return json(api_message(["time"=>date("Y-m-d H:i:s")], "操作成功"));
    }

    /**
     * 返回客服跳转地址
     * @return Json
     */
    public function getKfUrl(): Json
    {
        $url = Env('domain.kf_url');
        return json(api_message(['url' => $url], "操作成功"));
    }

    /**
     * Desc : PC端获取微信公众号二维码和盒子下载二维码
     * User : Steven
     * Date : 2024-07-05 09:37
     * @return Json
     */
    public function getWechatAndDownloadQrcode(): Json
    {
        $wechatQrcode = env('pc_config.wechat_public_account_qrcode');
        $downloadQrcode = env('pc_config.pc_box_download_qrcode');
        return json(api_message(['wechat_qrcode' => $wechatQrcode, 'download_qrcode' => $downloadQrcode], "获取成功"));
    }

    /**
     * Notes: IOS启动按钮开关
     * User: liubaoqiang
     * DateTime: 2024/12/19 19:58
     * @return Json
     */
    public function getIosStartOff(): Json
    {
        $start = env('start.ios_start_button');
        return json(api_message(['on'=>$start], "获取成功"));
    }

    /**
     * Notes: APP下载链接
     * User: liubaoqiang
     * DateTime: 2024/12/26 14:44
     * @return Json
     */
    public function download(): Json
    {
        $device = get_device_type();
        if($device == 1){
//            HomeService::plistDownload();
//            HomeService::setHtmlPlist();
            header("Location: https://app.clophone.com/index.html");
        }else{
            header("Location: http://download.clophone.com/clophone.apk");
        }
        exit();
    }
    public function demo()
    {
        header('Content-Type: application/xml');
        // 设置Content-Disposition为attachment，强制浏览器下载文件
        header('Content-Disposition: attachment; filename="example.plist"');
// 定义应用信息
        $appInfo = [
            'bundle_identifier' => 'com.clophone.zycloudbox',
            'bundle_version'    => '1.0',
            'app_title'         => '掌娱云手机',
            'ipa_url'           => 'http://download.clophone.com/FS7d55nICDKxlgd8.ipa', // 确保这是你的IPA文件的真实URL
        ];

// 创建一个新的DOMDocument对象
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->formatOutput = true;

// 添加根元素 <plist>
        $plist = $dom->createElement('plist');
        $plist->setAttribute('version', '1.0');
        $dom->appendChild($plist);

// 添加 <dict> 元素
        $dict = $dom->createElement('dict');
        $plist->appendChild($dict);

// 添加 items 数组
        $items = $dom->createElement('key', 'items');
        $dict->appendChild($items);
        $itemsArray = $dom->createElement('array');
        $dict->appendChild($itemsArray);

// 添加单个 item 的字典
        $itemDict = $dom->createElement('dict');
        $itemsArray->appendChild($itemDict);

// assets 数组
        $assetsKey = $dom->createElement('key', 'assets');
        $itemDict->appendChild($assetsKey);
        $assetsArray = $dom->createElement('array');
        $itemDict->appendChild($assetsArray);

// asset 字典
        $assetDict = $dom->createElement('dict');
        $assetsArray->appendChild($assetDict);

// kind 和 url
        $kindKey = $dom->createElement('key', 'kind');
        $kindString = $dom->createElement('string', 'software-package');
        $assetDict->appendChild($kindKey);
        $assetDict->appendChild($kindString);

        $urlKey = $dom->createElement('key', 'url');
        $urlString = $dom->createElement('string', $appInfo['ipa_url']);
        $assetDict->appendChild($urlKey);
        $assetDict->appendChild($urlString);

// metadata 字典
        $metadataKey = $dom->createElement('key', 'metadata');
        $itemDict->appendChild($metadataKey);
        $metadataDict = $dom->createElement('dict');
        $itemDict->appendChild($metadataDict);

// metadata 中的键值对
        foreach (['bundle_identifier', 'bundle_version', 'title'] as $key) {
            $metaKey = $dom->createElement('key', $key);
            $metaValue = $dom->createElement('string', $appInfo[$key == 'title' ? 'app_title' : $key]);
            $metadataDict->appendChild($metaKey);
            $metadataDict->appendChild($metaValue);
        }

// 输出或者保存 plist 文件
        echo $dom->saveXML();
    }

}

